package com.mall.web.control.app.code;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.biz.user.VerifyCodeBizImpl;
import com.mall.client.user.InviteCodeService;
import com.mall.client.user.UserService;
import com.mall.pojo.InviteCode;
import com.mall.pojo.User;
import com.mall.web.annotation.CheckCommmonOnlyKey;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.WebUtil;
import com.mall.web.vo.ResponseEntity;
import com.mall.web.vo.StatusInfo;




@Controller
@RequestMapping("/code/")
public class CodeControl {

	@Autowired
	private VerifyCodeBizImpl verifyCodeBiz;

	@Autowired
	private UserService userService;

	@Autowired
	private InviteCodeService inviteCodeService;

	/**

	 * 发送验证码

	 * 

	 * @param request

	 * @param response

	 */
	
	@RequestMapping("sendValidCode.json")
	@ResponseBody
	@CheckCommmonOnlyKey
	public ResponseEntity<String> sendValidCode(HttpServletRequest request, HttpServletResponse response) {

		try {
			// String callback = request.getParameter("callback");


			// f:忘记密码 r：注册
			Integer onlyKey = HttpUtil.getCommkeyId(request, response);

			String sourseType = request.getParameter("sourseType");
			String tuxingCode = request.getParameter("tuxingCode");
			if (org.apache.commons.lang3.StringUtils.isBlank(sourseType)) {
				sourseType = "f";
			}
			if ("f".equalsIgnoreCase(sourseType)) {
			} else {
				sourseType = "r";
			}
			String mobile = request.getParameter("phone");
			if (StringUtils.isBlank(mobile)) {
				ResponseEntity<String> responseEntity = new ResponseEntity<String>(StatusInfo.REQUEST_FAILURE,
						StatusInfo.FAILURE_MSG_MOBILE_NULL, null);
				return responseEntity;
			}
			mobile = mobile.trim();

			// String jsessionId = getJUSESSIONID(request);
			String sessionId = "";
			String a = request.getParameter("codesessionId");
			if (StringUtils.isNotBlank(a)) {
				sessionId = a;
			}else{
				sessionId = WebUtil.getSessionId(request);
			}

			InviteCode inviteCode = this.inviteCodeService.getInviteCodeBySessionIdAndType1(sessionId);
			if("r".equals(sourseType)){
				if (inviteCode == null || StringUtils.isBlank(inviteCode.getCode()) || StringUtils.isBlank(tuxingCode)
						|| !(tuxingCode.equals(inviteCode.getCode()))) {
					ResponseEntity<String> responseEntity = new ResponseEntity<String>("-200", "tuxingcode error", null);
					return responseEntity;
				}
			}
			

			if ("f".equalsIgnoreCase(sourseType)) {
				User user = this.userService.getUserByPhone(mobile,onlyKey);
				if (user == null) {
					ResponseEntity<String> responseEntity = new ResponseEntity<String>(StatusInfo.REQUEST_FAILURE,
							StatusInfo.REQUEST_CODESEND_USER_NOTEXIST_FAILURE_MSG, null);
					return responseEntity;
				}
			}
			
			if ("r".equalsIgnoreCase(sourseType)) {
				User user = this.userService.getUserByPhone(mobile,onlyKey);
				if (user != null) {
					ResponseEntity<String> responseEntity = new ResponseEntity<String>(StatusInfo.REQUEST_FAILURE,
							StatusInfo.REQUEST_CODESEND_USER_TEXIST_FAILURE_MSG, null);
					return responseEntity;
				}
			}

			String result = verifyCodeBiz.sendVerifyCode(mobile.trim());
			if (StringUtils.isEmpty(result)) {
				// ResponseEntity<String> responseEntity = new


				// ResponseEntity<>();


				// responseEntity.setCode("0");


				// responseEntity.setMessage("验证发送请求失败");


				// WebUtil.ajaxJsonpOutput(callback, responseEntity, response);


				ResponseEntity<String> responseEntity = new ResponseEntity<String>(StatusInfo.REQUEST_FAILURE,
						StatusInfo.REQUEST_FAILURE_MSG, null);
				return responseEntity;
			}

			if ("sendmsg error".equals(result)) {
				ResponseEntity<String> responseEntity = new ResponseEntity<String>("-201", "号码发送过于频发", null);
				return responseEntity;
			}
			// ResponseEntity<String> responseEntity = new ResponseEntity<>();


			// responseEntity.setCode("1");


			// responseEntity.setMessage("验证发送请求成功");


			// WebUtil.ajaxJsonpOutput(callback, responseEntity, response);


			ResponseEntity<String> responseEntity = new ResponseEntity<String>(StatusInfo.REQUEST_SUCCESS,
					StatusInfo.REQUEST_SUCCESS_MSG, result);
			return responseEntity;
		} catch (Exception e) {
			// String callback = request.getParameter("callback");


			// ResponseEntity<String> responseEntity = new ResponseEntity<>();


			// responseEntity.setCode("0");


			// responseEntity.setMessage("验证发送请求失败");


			// WebUtil.ajaxJsonpOutput(callback, responseEntity, response);


			ResponseEntity<String> responseEntity = new ResponseEntity<String>(StatusInfo.REQUEST_FAILURE,
					StatusInfo.REQUEST_FAILURE_MSG, null);
			return responseEntity;
		}
	}

	private String getJUSESSIONID(HttpServletRequest request) {
		String cookieValue = "";
		Cookie[] cookies = request.getCookies();// 这样便可以获取一个cookie数组

		if (cookies == null) {
			return "";
		}
		for (Cookie cookie : cookies) {
			if (cookie.getName().equalsIgnoreCase("JUSESSIONID")) {
				cookieValue = cookie.getValue();
				return cookieValue;
			}
		}
		return cookieValue;
	}
}
